/*
* Project Porcupine
* Copyright (C) 2012  Team Porcupine
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.towerdive.porcupine.maps;

import com.jme3.scene.Node;

/**
 * This class is responsible for generating a tile-based map. It's a suitable
 * superclass for things like caverns, dungeons, and building interiors.
 *
 * @author Martin "quill18" Glaude <quill18@towerdive.com>
 */
public abstract class TileMap extends GroundMap {

    /**
     * Walls and such will ultimately be managed as simple objects,
     * but for the TileMap generation we will need a tilemap.
     */
    protected enum TileType {
            EMPTY, WALL, PLAYER_START, MONSTER
    }

    protected static class Tile {
            int x, y;	// We need to know our position for adjacency.
            TileType tileType;	// For now: 0=empty, 1=wall
            public Tile(int x, int y, TileType tileType) {
                    this.x = x;
                    this.y = y;
                    this.tileType = tileType;
            }
    }

    public TileMap(Node mapNode, int width, int height, float tileWidth, float tileHeight) {
            super(mapNode);
            this.width = width;
            this.height = height;
            this.tileWidth = tileWidth;
            this.tileHeight = tileHeight;

            tiles = new Tile[width][height];
    }
    
    protected Tile[][] tiles;

    protected int width;
    protected int height;

    protected float tileWidth;
    protected float tileHeight;

    @Override
    public float getTotalWidth() { return width * tileWidth; }

    @Override
    public float getTotalHeight() { return height * tileHeight; }

    /**
     * Converts our internal tile map to 3D map objects
     */
    protected void GenerateMapObjects() {
        getMapObjects().add( new MapObject(
                         0, -0.01f, getTotalHeight(), 
                         MapObject.ObjectType.FLOOR) );

        for(int x = 0; x < width; x++) {
            for(int y=0; y < height; y++) {
                switch(tiles[x][y].tileType) {
                case WALL:
                    getMapObjects().add( new MapObject(
                            x*tileWidth, 0, y*tileWidth, 
                            MapObject.ObjectType.WALL) );
                    break;
                case PLAYER_START:
                    getMapObjects().add( new MapObject(
                            x*tileWidth, 0, y*tileWidth, 
                            MapObject.ObjectType.PLAYER_START) );
                    break;
                case MONSTER:
                    getMapObjects().add( new MapObject(
                            x*tileWidth, 0, y*tileWidth, 
                            MapObject.ObjectType.MONSTER) );
                    break;
                }
            }
        }
    }

}
